package com.prs.crm.action.fs;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;

import com.prs.crm.context.Constrains;
import com.prs.crm.domain.fs.Acct;
import com.prs.crm.domain.fs.AcctType;
import com.prs.crm.service.fs.AcctService;
import com.prs.crm.service.fs.AcctTypeService;

public class AcctAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Acct acct;
	private AcctService acctService;
	private AcctTypeService acctTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Collection<TreeNode> nodes;
	private Collection<AcctType> acctTypes;
	private Acct parentAcct;
	private String selectMold;

	@Action("treeAcct")
	public String treeAcct() {
		acctTypes = this.getAcctTypeService().getAllAcctType();
		return SUCCESS;
	}

	@Action(value = "loadAcctTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadAcctTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<Acct> assets = this.getAcctService().getRootAcctByMold(
				Constrains.ACCT_MOLD_ASSETS);
		Collection<Acct> costs = this.getAcctService().getRootAcctByMold(
				Constrains.ACCT_MOLD_COST);
		Collection<Acct> equities = this.getAcctService().getRootAcctByMold(
				Constrains.ACCT_MOLD_EQUITIES);
		Collection<Acct> liabilites = this.getAcctService().getRootAcctByMold(
				Constrains.ACCT_MOLD_LIABILITIES);
		Collection<Acct> profitandloss = this.getAcctService()
				.getRootAcctByMold(Constrains.ACCT_MOLD_PROFITANDLOSS);

		TreeNode assetsNode = new TreeNode();
		assetsNode.setId("-11");
		assetsNode.setText(Constrains.ACCT_MOLD_ASSETS);
		nodes.add(assetsNode);
		for (Acct acct : assets) {
			TreeNode node = new TreeNode();
			node.setId(acct.getId().toString());
			node.setText(acct.getName());
			node.setValue(acct.getMold());
			assetsNode.addChildNode(node);
			addChildNode(acct, node);
		}

		TreeNode costsNode = new TreeNode();
		costsNode.setId("-12");
		costsNode.setText(Constrains.ACCT_MOLD_COST);
		nodes.add(costsNode);
		for (Acct acct : costs) {
			TreeNode node = new TreeNode();
			node.setId(acct.getId().toString());
			node.setText(acct.getName());
			node.setValue(acct.getMold());
			costsNode.addChildNode(node);
			addChildNode(acct, node);
		}

		TreeNode equitiesNode = new TreeNode();
		equitiesNode.setId("-13");
		equitiesNode.setText(Constrains.ACCT_MOLD_EQUITIES);
		nodes.add(equitiesNode);
		for (Acct acct : equities) {
			TreeNode node = new TreeNode();
			node.setId(acct.getId().toString());
			node.setText(acct.getName());
			node.setValue(acct.getMold());
			equitiesNode.addChildNode(node);
			addChildNode(acct, node);
		}

		TreeNode liabilitesNode = new TreeNode();
		liabilitesNode.setId("-14");
		liabilitesNode.setText(Constrains.ACCT_MOLD_LIABILITIES);
		nodes.add(liabilitesNode);
		for (Acct acct : liabilites) {
			TreeNode node = new TreeNode();
			node.setId(acct.getId().toString());
			node.setText(acct.getName());
			node.setValue(acct.getMold());
			liabilitesNode.addChildNode(node);
			addChildNode(acct, node);
		}

		TreeNode profitandlossNode = new TreeNode();
		profitandlossNode.setId("-15");
		profitandlossNode.setText(Constrains.ACCT_MOLD_PROFITANDLOSS);
		nodes.add(profitandlossNode);
		for (Acct acct : profitandloss) {
			TreeNode node = new TreeNode();
			node.setId(acct.getId().toString());
			node.setText(acct.getName());
			node.setValue(acct.getMold());
			profitandlossNode.addChildNode(node);
			addChildNode(acct, node);
		}

		return SUCCESS;
	}

	@Action(value = "loadAcct", results = { @Result(name = "success", type = "json", params = {
			"root", "acct", "excludeProperties", "type,childs,upAcct" }) })
	public String loadAcct() {
		return SUCCESS;
	}

	private void addChildNode(Acct parent, TreeNode parentNode) {
		if (parent.getChilds() != null) {
			for (Acct acct : parent.getChilds()) {
				TreeNode node = new TreeNode();
				node.setId(acct.getId().toString());
				node.setText(acct.getName());
				node.setValue(acct.getMold());
				parentNode.addChildNode(node);
				addChildNode(acct, node);
			}
		}
	}

	@Action(value = "saveAcct", results = {
			@Result(name = "success", type = "json", params = { "root", "acct",
					"excludeProperties", "type,childs,upAcct" }),
			@Result(name = "error", type = "chain", location = "treeAcct") })
	public String saveAcct() {
		if (acct.getName() == null || "".equals(acct.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (acct.getCode() == null || acct.getCode().equals("")) {
			addActionError("编码不能为空");
			return ERROR;
		}
		if (acct.getFlag() == null || "".equals(acct.getFlag().trim())) {
			addActionError("方向不能为空");
			return ERROR;
		}
		if (acct.getMark() == null || "".equals(acct.getMark().trim())) {
			addActionError("速记代码不能为空");
			return ERROR;
		}
		if (acct.getType() == null) {
			addActionError("类型能为空");
			return ERROR;
		}
		if (acct.getId() == null) {
			acct.setUpAcct(parentAcct);
			acct.setMold(selectMold);
		}
		this.getAcctService().save(acct);

		return SUCCESS;
	}

	@Action(value = "removeAcct", results = { @Result(name = "success", type = "json", params = {
			"root", "prompt" }) })
	public String removeAcct() {
		try {
			this.getAcctService().remove(acct);
			this.setPrompt("success");
		} catch (Exception e) {
			// TODO: handle exception
			this.setPrompt("error");
		}
		return SUCCESS;
	}

	public Acct getAcct() {
		return acct;
	}

	public void setAcct(Acct acct) {
		this.acct = acct;
	}

	public AcctService getAcctService() {
		return acctService;
	}

	public void setAcctService(AcctService acctService) {
		this.acctService = acctService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public AcctTypeService getAcctTypeService() {
		return acctTypeService;
	}

	public void setAcctTypeService(AcctTypeService acctTypeService) {
		this.acctTypeService = acctTypeService;
	}

	public Collection<AcctType> getAcctTypes() {
		return acctTypes;
	}

	public void setAcctTypes(Collection<AcctType> acctTypes) {
		this.acctTypes = acctTypes;
	}

	public void setParentAcct(Acct parentAcct) {
		this.parentAcct = parentAcct;
	}

	public Acct getParentAcct() {
		return parentAcct;
	}

	public void setSelectMold(String selectMold) {
		this.selectMold = selectMold;
	}

	public String getSelectMold() {
		return selectMold;
	}

}
